package com.manner.groupon.controller.other;

import com.alibaba.fastjson.JSONObject;
import com.manner.groupon.service.pay.PayService;
import com.manner.groupon.service.pay.impl.PayServiceImpl;
import com.manner.groupon.utils.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/pay")
@Slf4j
    
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping("/callback")
    public String callback(@RequestBody JSONObject req) {
        Map<String, Object> params = req.toJavaObject(Map.class);
        params.forEach((k,v) -> {
            log.info("key为 : {}, value为 : {}", k, v);
        });
        //验证异步通知请求签名
        Boolean verify = StrUtil.verify(params, PayServiceImpl.MD5_KEY);

        //返回success字符串，否则支付系统将重复推送通知，间隔通知频率为0/30/60/90/120/150,单位：秒
        return payService.callback(params.get("mchOrderNo").toString(), params.get("payOrderId").toString());
    }

    @PostMapping("/refund")
    public String refund(@RequestBody JSONObject req) {
        Map<String, Object> params = req.toJavaObject(Map.class);
        params.forEach((k,v) -> {
            log.info("key为 : {}, value为 : {}", k, v);
        });
        //验证异步通知请求签名
        Boolean verify = StrUtil.verify(params, PayServiceImpl.MD5_KEY);

        return payService.orderRefund(params.get("payOrderId").toString(), Integer.valueOf(params.get("state").toString()));
    }
}
